﻿using System;
using NEnsure.Rules;

namespace NEnsure
{
	public class Rule
	{
		protected Rule()
		{

		}

		public static IBusinessRule IsRequired = new RequiredRule<string>();
		public static IBusinessRule IsAlphaOnly = new AlphaOnlyRule();
		public static IBusinessRule IsNumericOnly = new NumericOnlyRule();
		public static IBusinessRule IsAlphaNumeric = new AlphaNumericRule();
		public static IBusinessRule ContainsAlpha = new ContainsAlphaRule();
		public static IBusinessRule ContainsDigit = new ContainsDigitRule();
		public static IBusinessRule IsUsername = new UsernameRule();
		public static IBusinessRule IsEmail = new EmailRule();
		public static IBusinessRule IsUSPhone = new USPhoneRule();
		public static IBusinessRule IsUSZipCode = new USZipCodeRule();
		public static IBusinessRule IsSSN = new SSNRule();
		public static IBusinessRule IsUrlPart = new UrlPartRule();

		public static IBusinessRule Matches(string pattern)
		{
			return new RegexRule(pattern);
		}

		public static IBusinessRule HasMinLengthOf(int minLength)
		{
			return new MinLengthRule(minLength);
		}

		public static IBusinessRule HasMaxLengthOf(int maxLength)
		{
			return new MaxLengthRule(maxLength);
		}

		public static IBusinessRule HasMinValueOf<T>(T minValue) where T : IComparable<T>
		{
			return new MinValueRule<T>(minValue);
		}

		public static IBusinessRule HasMaxValueOf<T>(T maxValue) where T : IComparable<T> 
		{
			return new MaxValueRule<T>(maxValue);
		}

		public static IBusinessRule IsSameAs(string propertyName, string propertyValue)
		{
			return IsSameAs(propertyName, propertyValue, false);
		}

		public static IBusinessRule IsSameAs(string propertyName, string propertyValue, bool ignoreCase)
		{
			return new SameAsRule(propertyName, propertyValue, ignoreCase);
		}

		public static IBusinessRule IsEqualTo(string value)
		{
			return new EqualToRule<string>(value);
		}

		public static IBusinessRule GetRequiredRuleForType<T>()
		{
			return new RequiredRule<T>();			
		}

		public static IBusinessRule GetSameAsRuleForType<T>(string propertyName, T propertyValue) where T : IComparable<T>
		{
			return new SameAsRule<T>(propertyName, propertyValue);
		}
		
		public static IBusinessRule GetEqualToRuleForType<T>(T value)
		{
			return new EqualToRule<T>(value);
		}

		public static IBusinessRule IsBrokenIf<T>(IInspector<T> inspector)
		{
			return new ManualValidationRule<T>(inspector);
		}

		public static IBusinessRule IsUnique(Predicate<string> isInUseChecker)
		{
			return new UniqueRule(isInUseChecker);
		}
	}
}
